import axios from 'axios'
// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, 
  timeout: 12000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent

    config.headers['accessToken'] = 'eyJzdWIiOiIyMjQwYTI5MC1kZmYzLTExZWMtYjExZi0xNWVhOWNhNGFjMjEiLCJleHAiOjE2NjI0Mjc4ODIsImF1ZCI6Mjg0fQ==.0d8cdfc2d7cbfbbfa0454d3c32e1d30e9639121f592c20546fa1df8798ec3ee3'
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)
service.interceptors.response.use(
  response => {
    const res = response.data

    // if the custom code is not 20000, it is judged as an error.
    if (res.code&&res.code !== 200) {
      return Promise.reject(new Error(res.message || 'Error'))
    }else {
      return res
    }
  },
  error => {
    return Promise.reject(error)
  }
)

export default service
